<?php
namespace app\common\model;

use think\Model;

class SmsMobile extends Model
{
    protected $table = 'sms_mobile';

    public function getSentInfo($mobile, $scene = 'register') {
        $sms_res = $this->where('mobile', $mobile)
        ->where('date', date('Y-m-d'))
        ->where('status', 1)
        ->where('scene',$scene)
        ->order('create_time DESC')
        ->select();

        $last_time = 0;
        if(!empty($sms_res)) {
            $last_time = strtotime($sms_res[0]->create_time);
        }

        return ['total'=>count($sms_res), 'last_time'=>$last_time];
    }

    /*
     * 检验验证码
     */
    public function validateCode($mobile, $code, $scene='register') {
        $validate_res = ['state'=>true, 'msg'=>'验证成功'];
        $res = $this->where(['mobile'=>$mobile, 'date'=>date('Y-m-d'), 'code'=>$code, 'scene'=>$scene])
                ->where('expire_time', '>=', date('Y-m-d H:i:s'))
                ->find();

        if(is_null($res)) {
            $validate_res['state'] = false;
            $validate_res['msg'] = '验证码不存在或已失效';
        }
        return $validate_res;
    }

    /*
     * 结束场景验证码
     */
    public function finishCode($mobile, $scene='register') {
        return $this->where('mobile', $mobile)->where('date', date('Y-m-d'))->where('scene',$scene)->update(['is_del'=>1]);
    }
}